﻿using System.Windows;
using WaitLess.App.ViewModels;
using System.Windows.Controls;
using System;
using System.Windows.Media.Imaging;

namespace WaitLess.App.Views
{
    public partial class MainPage : PhoneApplicationPageBase
    {
        public MainViewModel ViewModel
        {
            get
            {
                return DataContext as MainViewModel;
            }
        }

        public MainPage()
        {
            InitializeComponent();

            DataContext = ContainerInitializer.Get<MainViewModel>();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModel.LoadData();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            LoadDefaultMunicipalOfficeImage(sender as Image);
        }

        private static void LoadDefaultMunicipalOfficeImage(Image image)
        {
            var defaultImage = new BitmapImage(new Uri(@"/Assets/MunicipalOffices/Default.png", UriKind.Relative));

            image.Source = defaultImage;
        }
    }
}